package cn.qdhlm.collectservice.core.component.command;

import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.util.CodeUtil;
import cn.qdhlm.collectservice.common.util.ComUtil;
import cn.qdhlm.collectservice.common.util.JsonUtil;
import cn.qdhlm.collectservice.model.dto.CommandParamDTO;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.log4j.Log4j2;

/**
 * @Name: CmdAfn04
 * @Description: 设置参数（AFN=04H）
 * @Author: jiangcs
 */
@Log4j2
public class CmdAfn04 {
    /**
     * 设置所有终端采集周期
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f12(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getCollectCycle())) {
            log.error("f12 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(6);
        // DT=> F12
        byteBuf.writeShort(0x0801);
        // SN
        byteBuf.writeShortLE(sn);
        // 抄表周期
        byteBuf.writeShortLE(commandParamDTO.getCollectCycle());
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 采集器校时
     *
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f16(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getYear()) || ComUtil.isEmpty(commandParamDTO.getMonth()) || ComUtil.isEmpty(commandParamDTO.getDay())
                || ComUtil.isEmpty(commandParamDTO.getHour()) || ComUtil.isEmpty(commandParamDTO.getMinute()) || ComUtil.isEmpty(commandParamDTO.getSecond())) {
            log.error("f16 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(10);
        // DT=> F16
        byteBuf.writeShort(0x8001);
        // SN
        byteBuf.writeShort(sn);
        // second
        byteBuf.writeByte(CodeUtil.hexStringToByte(ComUtil.fillZeroLeft(commandParamDTO.getSecond(), 2), 0));
        // minute
        byteBuf.writeByte(CodeUtil.hexStringToByte(ComUtil.fillZeroLeft(commandParamDTO.getMinute(), 2), 0));
        // hour
        byteBuf.writeByte(CodeUtil.hexStringToByte(ComUtil.fillZeroLeft(commandParamDTO.getHour(), 2), 0));
        // day
        byteBuf.writeByte(CodeUtil.hexStringToByte(ComUtil.fillZeroLeft(commandParamDTO.getDay(), 2), 0));
        // month
        byteBuf.writeByte(CodeUtil.hexStringToByte(ComUtil.fillZeroLeft(commandParamDTO.getMonth(), 2), 0));
        // year
        byteBuf.writeByte(CodeUtil.hexStringToByte(ComUtil.fillZeroLeft(commandParamDTO.getYear(), 2), 0));
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 配置信息初始化
     *
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f18(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getUrl())) {
            log.error("f18 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(6 + commandParamDTO.getUrl().length());
        // DT=> F18
        byteBuf.writeShort(0x0202);
        // SN
        byteBuf.writeShort(sn);
        // url
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getUrl())));
        // 结束 0x00
        byteBuf.writeByte(0x00);
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 系统升级
     *
     * @param sn
     * @param commandParamDTO
     * @return
     */
    public static ByteBuf f21(int sn, CommandParamDTO commandParamDTO) {
        if (ComUtil.isEmpty(commandParamDTO.getDomain()) || ComUtil.isEmpty(commandParamDTO.getFile())) {
            log.error("f21 参数不合法:{}", JsonUtil.safeJsonStr(commandParamDTO));
            return null;
        }

        ByteBuf byteBuf = Unpooled.buffer(4 + commandParamDTO.getDomain().length() + 1 + commandParamDTO.getFile().length() + 1);
        // DT=> F21
        byteBuf.writeShort(0x1002);
        // SN
        byteBuf.writeShort(sn);
        // domain
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getDomain())));
        // 分隔符
        byteBuf.writeByte(CodeUtil.hexStringToByte(CodeUtil.asciiStringToHexString(";"), 0));
        // file
        byteBuf.writeBytes(CodeUtil.hexStringToByteArray(CodeUtil.asciiStringToHexString(commandParamDTO.getFile())));
        // 结束 0x00
        byteBuf.writeByte(0x00);
        return Unpooled.wrappedBuffer(packDefault(commandParamDTO.getDeviceType()), byteBuf);
    }

    /**
     * 命令包：默认
     *
     * @param deviceType
     * @return
     */
    private static ByteBuf packDefault(int deviceType) {
        ByteBuf byteBuf = Unpooled.buffer(10);
        // 控制域
        byteBuf.writeByte(0x00);
        // 设备类型
        byteBuf.writeByte(deviceType);
        // 协议版本
        byteBuf.writeShort(0x0100);
        // 主站地址和组地址
        byteBuf.writeShort(0x0000);
        // AFN=04：设置参数
        byteBuf.writeByte(0x04);
        // 帧序列域SEQ(保留0xff)
        byteBuf.writeByte(0xFF);
        // 信息点DA(暂时使用0x00,0x00)
        byteBuf.writeShort(0x0000);
        return byteBuf;
    }
}
